﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Net.Http;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Web.Http.ModelBinding;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.OAuth;
using ezCloud.Api.Models;
using ezCloud.Api.Providers;
using ezCloud.Api.Results;
using ezCloud.Data;
using System.Net;
using ezCloud.Api.Common;


namespace ezCloud.Api.Controllers
{
    [Authorize]
    [RoutePrefix("api/RoomType")]
    public class RoomTypeController : ezController
    {
        private ApplicationUserManager _userManager;

        public ApplicationUserManager UserManager
        {
            get
            {
                return _userManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
            }
            private set
            {
                _userManager = value;
            }
        }

       /* [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("SaveRoomTypes")]
        public async Task<IHttpActionResult> SaveRoomTypes(RoomTypeViewModel[] model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            
            var currentClientUser = Common.UserUtitily.getUserProfile();

            //List<RoomTypes> deleteRoomTypeList = context.RoomTypes.Where(roomType => roomType.HotelId == currentClientUser.HotelId).ToList();
            //for (int i=0;i<deleteRoomTypeList)

           
            for (int i = 0; i < model.Length; i++)
            {
                RoomTypes roomType = new Data.RoomTypes();
                roomType.RoomTypeName = model[i].RoomTypeName;
                roomType.RoomTypeCode = model[i].RoomTypeCode;
                roomType.RoomTypeDescription = model[i].RoomTypeDescription;
                roomType.HotelId = currentClientUser.HotelId;
                roomType.Image = model[i].Image;
                roomType.Price = model[i].Price;
                context.RoomTypes.InsertOnSubmit(roomType);
                context.SubmitChanges();
            }


                return Ok();
        }*/

        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("SaveRoomType")]
        public async Task<IHttpActionResult> SaveRoomType(RoomTypeViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            
            var currentClientUser = Common.UserUtitily.getUserProfile();

            RoomTypes currentRoomType = context.RoomTypes.Where(roomType => roomType.RoomTypeId == model.RoomTypeId).Single();
            currentRoomType.RoomTypeName = model.RoomTypeName;
            currentRoomType.RoomTypeCode = model.RoomTypeCode;
            currentRoomType.RoomTypeDescription = model.RoomTypeDescription;
            currentRoomType.Image = model.Image;
            currentRoomType.Price = model.Price;
            currentRoomType.HotelId = currentClientUser.HotelId;
            context.SubmitChanges();
           
            return Ok();
        }

        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("CreateRoomType")]
        public async Task<IHttpActionResult> CreateRoomType(RoomTypeViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            
            var currentClientUser = Common.UserUtitily.getUserProfile();

            RoomTypes newRoomTypes = new Data.RoomTypes();
            newRoomTypes.RoomTypeName = model.RoomTypeName;
            newRoomTypes.RoomTypeCode = model.RoomTypeCode;
            newRoomTypes.RoomTypeDescription = model.RoomTypeDescription;
            newRoomTypes.Image = model.Image;
            newRoomTypes.Price = model.Price;
            newRoomTypes.HotelId = currentClientUser.HotelId;
            context.RoomTypes.InsertOnSubmit(newRoomTypes);
            context.SubmitChanges();

            return Ok(newRoomTypes);
        }


        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("GetAllRoomTypes")]
        public List<RoomTypes> GetAllRoomTypes()
        {
            var user = Common.UserUtitily.getUserProfile();
            
            return context.RoomTypes.Where(roomType => roomType.HotelId == user.HotelId).ToList();
        }

        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("RoomTypeByRoom")]
        public RoomTypes getRoomTypeByRoom(int roomId)
        {
            var currentClientUser = Common.UserUtitily.getUserProfile();
            
            Rooms room = context.Rooms.Where(rm => rm.HotelId == currentClientUser.HotelId && rm.RoomId == roomId).FirstOrDefault();

            if (room == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            return context.RoomTypes.Where(roomType => roomType.RoomTypeId == room.RoomTypeId).FirstOrDefault();
        }

    }
}